/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.ui;

import fait.ferm.global.Glob;
import fait.ferm.global.TB;
import fait.ferm.persists.Cow;
import fait.ferm.persists.Cow_group;
import fait.ferm.persists.Cow_type;
import fait.ferm.persists.Milk_store;
import fait.ferm.persists.Plan;
import fait.ferm.persists.Plan_details_care;
import fait.ferm.persists.Plan_details_care_ex;
import fait.ferm.persists.Resource_counter;
import fait.ferm.persists.Supplies_store;
import fait.ferm.tools.Tools;
import java.net.URL;
import java.sql.Date;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;

/**
 *
 * @author alex
 */
public class SuppliesMilkQuantityController   implements Initializable{

	@FXML
	TableView<TB.Table3Col> supTab; 
	@FXML
	TableView<TB.Table4Col> milkTab; 
	
	
	@Override
	public void initialize(URL url, ResourceBundle rb) {
		fillSupplies();
		fillMilk();
	}
	
    private void fillSupplies(){
		ArrayList<Supplies_store> ar = (ArrayList<Supplies_store>) Glob.getFermEngine().getSuppliesQuantity().obj;
		ObservableList<TB.Table3Col> items = FXCollections.observableArrayList();
		for(Supplies_store rc: ar){
		
            ArrayList<Plan> ar_plan = (ArrayList<Plan>)Glob.getFermEngine().getCurrentPlan().obj;
			double val = 0.0; //сколько нужно до конца месяца
            
			for(Plan cur_plan: ar_plan) {
                
                double _val = 0.0; //запланированное для групп
                ArrayList<Plan_details_care> ar_plan_details_care = (ArrayList<Plan_details_care>)Glob.getFermEngine().getPlanDetailsCareFromPlan(cur_plan).obj;
                for(Plan_details_care cur_plan_details_care: ar_plan_details_care) {
                    
                    if(cur_plan_details_care.supplies_id == rc.supplies_id.getId()) {
                        
                        _val += cur_plan_details_care.quantity;
                        
                    }
                    
                }
                double _val_ex = 0.0; //разовое
                ArrayList<Plan_details_care_ex> ar_plan_details_care_ex = (ArrayList<Plan_details_care_ex>)Glob.getFermEngine().getPlanDetailsCareExFromPlan(cur_plan).obj;
                for(Plan_details_care_ex cur_plan_details_care_ex: ar_plan_details_care_ex) {
                    
                    if(cur_plan_details_care_ex.supplies_id == rc.supplies_id.getId()) {
                        
                        _val_ex += cur_plan_details_care_ex.quantity;
                        
                    }
                    
                }
                val = _val * (31 - Integer.parseInt(Glob.getGame().current_date.toString().substring(8, 10)))/Tools.millisecundsToDay(Glob.getGame().current_date.getTime() - cur_plan.date_beg.getTime());
                
                val += _val_ex;
                
            }
			
			TB.Table3Col ob = new TB.Table3Col(rc.supplies_id.getName(), String.format("%.2f", rc.quantity), String.format("%.2f", val));
			items.add(ob);
		}
		TB.bind(supTab);
		supTab.setItems(items);
		
	}

	private void fillMilk(){
		ArrayList<Milk_store> ar = (ArrayList<Milk_store>) Glob.getFermEngine().getMilkQuantity().obj;
		ObservableList<TB.Table4Col> items = FXCollections.observableArrayList();
		for(Milk_store rc: ar){
		
			double val = 0.0;
			ArrayList<Cow_group> ar_cow_group = (ArrayList<Cow_group>)Glob.getFermEngine().getCowGroup().obj;
            for(Cow_group cur_group: ar_cow_group) {
                
                ArrayList<Cow> ar_cow = (ArrayList<Cow>)Glob.getFermEngine().getCowFromGroup(cur_group.getId()).obj;
                for(Cow cur_cow: ar_cow) {
                    
                    try {
                        Cow_type cur_type = new Cow_type(cur_group.cow_type.getId());
                        if(cur_type.milk_sort == rc.sort_id.getId()) {

                                val += cur_type.milk_yeild;

                        }
                    } catch(Exception ex) {
                        Logger.getLogger(SuppliesMilkQuantityController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }
                
            }
            
            val *= (31 - Integer.parseInt(Glob.getGame().current_date.toString().substring(8, 10)));
            
            
			double val2; //сколько продадим до конца месяца
			//TODO сделать нормальный расчет прогноза
			val2 = Math.random()*1000;
			
			TB.Table4Col ob = new TB.Table4Col(rc.sort_id.getName(), String.format("%.2f", rc.quantity), String.format("%.2f", val),String.format("%.2f", val2));
			items.add(ob);
            
		}
		TB.bind(milkTab);
		milkTab.setItems(items);
		
	}
	
}
